package com.zm.zmgz.service;


import com.zm.zmgz.annotationzm.InformationProhibitedDisplayItem;
import com.zm.zmgz.dao.AuthorityBeanMapper;
import com.zm.zmgz.dao.EmployeeBeanMapper;
import com.zm.zmgz.dao.UserBeanMapper;
import com.zm.zmgz.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.util.*;

@Service
public class UserService {

    @Autowired
    UserBeanMapper userBeanMapper;

    @Autowired
    EmployeeBeanMapper employeeBeanMapper;

    @Autowired
    AuthorityBeanMapper authorityBeanMapper;

    public UserBean getUserByUnameAndPassword(String uname, String password) {
        UserBeanExample userBeanExample = new UserBeanExample();
        UserBeanExample.Criteria criteria = userBeanExample.createCriteria();
        criteria.andUsernameEqualTo(uname).andPasswordEqualTo(password);
        List<UserBean> userBeans = userBeanMapper.selectByExample(userBeanExample);
        UserBean userBean;
        if(userBeans.size()==0){
            userBean = new UserBean();
            userBean.setMsg("账号或密码错误!");
        } else {
            userBean = userBeans.get(0);
            userBean.setCode(20000); // 20000自定义状态码表示一切正常
            userBean.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif"); // 设置用户头像
            userBean.setToken(userBean.getUsername()); // admin为获取token信息的参数值
        }
        return userBean;
    }

    public List<UserBean> getUsers() { // 获取所有的用户对象
        System.out.println(userBeanMapper.getUsers());
        return userBeanMapper.getUsers();
    }

    public List<Map<String, String>> getAvailabledentificationnumbers() { // 注册用户时,获取可用身份证列表
        List<EmployeeBean> list = employeeBeanMapper.getAvailabledentificationnumbers();
        List<Map<String, String>> list1 = new ArrayList<>();
        for (EmployeeBean employeeBean : list) {
            Map<String, String> stringMap = new HashMap<>();
            stringMap.put("value",employeeBean.getIdentificationnumber());
            stringMap.put("label",employeeBean.getIdentificationnumber()+"("+employeeBean.getName()+")");
            list1.add(stringMap);
        }
        return list1;
    }

    public int addUser(UserBean userBean) { // 添加一个用户
        return userBeanMapper.insert(userBean);
    }


    public int editUser(UserBean userBean) { // 编辑1个用户的信息
        UserBeanExample example = new UserBeanExample();
        UserBeanExample.Criteria criteria = example.createCriteria();
        criteria.andIdentificationnumberEqualTo(userBean.getIdentificationnumber());
        UserBean userBean1 = userBeanMapper.selectByExample(example).get(0); // 根据传入对象的身份证号从数据库中查询出该对象
        Class<? extends UserBean> aClass = userBean.getClass(); // 获取员工对象的class对象
        Field[] declaredFields = aClass.getDeclaredFields(); // 获取员工对象的所有声明属性
        for (Field declaredField : declaredFields) { // 遍历员工对象的所有声明属性
            declaredField.setAccessible(true); // 取消对属性的安全检查
            if(!declaredField.isAnnotationPresent(InformationProhibitedDisplayItem.class)){ // 如果该属性没有被该注解标注,表示该属性允许被修改
                try {
                    if(declaredField.get((Object) userBean) != declaredField.get(userBean1)){ // 如果该属性被修改了
                        declaredField.set(userBean1,declaredField.get(userBean)); // 属性值更新
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return userBeanMapper.updateByPrimaryKey(userBean1);
    }

    public int deleteUser(UserBean userBean) { // 删除一个用户
        return userBeanMapper.deleteByPrimaryKey(userBean.getId());
    }

    @Transactional
    public Boolean editAuthorityData2(AuthorityData2Bean authorityData2Bean) { // 更新权限(先删除,后插入)
        // 把该用户的权限全部删除
        AuthorityBeanExample authorityBeanExample = new AuthorityBeanExample();
        AuthorityBeanExample.Criteria criteria = authorityBeanExample.createCriteria();
        criteria.andIdentificationnumberEqualTo(authorityData2Bean.getIdentificationnumber());
        int i = authorityBeanMapper.deleteByExample(authorityBeanExample);
        // 新增该用户的权限
        List<String> string = authorityData2Bean.getAuthorityArray();
        for (String s : string) {
            AuthorityBean authorityBean = new AuthorityBean();
            authorityBean.setIdentificationnumber(authorityData2Bean.getIdentificationnumber());
            authorityBean.setPath(s);
            int insert = authorityBeanMapper.insert(authorityBean);
        }
        return true;
    }

    public AuthorityData2Bean getAuthorityData2(String identificationnumber) { // 获取权限信息
        AuthorityBeanExample authorityBeanExample = new AuthorityBeanExample();
        AuthorityBeanExample.Criteria criteria = authorityBeanExample.createCriteria();
        criteria.andIdentificationnumberEqualTo(identificationnumber);
        List<AuthorityBean> list = authorityBeanMapper.selectByExample(authorityBeanExample); // 通过身份证获取所有权限信息
        AuthorityData2Bean authorityData2Bean = new AuthorityData2Bean();
        authorityData2Bean.setIdentificationnumber(identificationnumber);
        authorityData2Bean.setAuthorityArray(new ArrayList<String>());
        if(list.size()>0) {
            for (AuthorityBean authorityBean : list) {
                authorityData2Bean.getAuthorityArray().add(authorityBean.getPath());
            }
        }

        return authorityData2Bean;
    }

    /***
    * @Description: 通过身份证获取用户名
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-03-25 11:01:25
    */
    public String getUserNameByidentificationNumber(String identificationNumber) {
        UserBeanExample userBeanExample = new UserBeanExample();
        UserBeanExample.Criteria criteria = userBeanExample.createCriteria();
        criteria.andIdentificationnumberEqualTo(identificationNumber);
        List<UserBean> userBeans = userBeanMapper.selectByExample(userBeanExample);
        if(userBeans.size() > 0) {
            String username = userBeans.get(0).getUsername();
            System.out.println(identificationNumber+"用户名为:"+username);
            return username;
        } else {
            System.out.println("暂无身份证为"+identificationNumber+"用户");
            return null;
        }
    }

    /***
    * @Description: 获取所有的用户名
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-04-08 01:31:24
    */
    public List<String> getAllUserName() {
        UserBeanExample userBeanExample = new UserBeanExample();
        List<UserBean> userBeans = userBeanMapper.selectByExample(userBeanExample);
        List<String> list = new ArrayList<>();
        if(!Objects.isNull(userBeans) && userBeans.size() > 0) {
            for (UserBean userBean : userBeans) {
                list.add(userBean.getUsername());
            }
            return list;
        } else {
            System.err.println("获取用户名集合失败!");
            return null;
        }
    }

}
